package com.library.verification.view;

import java.util.Random;

/**
 * Created by zhoulikai on 17/4/24.
 * 验证码生成器
 */

public class CheckCodeGenerator implements ICheckCode {
    //可选的字符范围
    private static final String codes = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";

    @Override
    public String makeCheckCode(int length) {
        if (length == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String randomCode = generateRandomCode();
            sb.append(randomCode);
        }
        return sb.toString();
    }


    private String generateRandomCode() {
        String code = "A";
        int length = codes.length();

        Random r = new Random();
        int index = r.nextInt(length);
        if (index >= 0 && index < length) {
            code = (codes.charAt(index) + "");
        }
        return code;
    }

}
